package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.CompanyApi;
import com.sd365.permission.centre.entity.Company;
import com.sd365.permission.centre.pojo.dto.CompanyDTO;
import com.sd365.permission.centre.pojo.query.CompanyQuery;
import com.sd365.permission.centre.pojo.query.IdVersionQuery;
import com.sd365.permission.centre.pojo.vo.CompanyVO;
import com.sd365.permission.centre.service.CompanyService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author jxd
 * @Date 2020/12/12  1:05 下午
 * @Version 1.0
 * @Write For CompanyController
 * @Email waniiu@126.com
 */
@RestController
public class CompanyController implements CompanyApi {

    @Resource
    CompanyService companyService;

    /**
     * 通用查询，带分页信息
     *
     * @param companyQuery
     * @return
     */
    @Override
    @ApiLog
    public List<CompanyVO> commonQuery(CompanyQuery companyQuery) {
        List<Company> companies = companyService.commonQuery(companyQuery);
        List<CompanyVO> companyVOS;
        try {
            companyVOS = BeanUtil.copyList(companies, CompanyVO.class);
        } catch (Exception e) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, e);
        }
        return companyVOS;
    }

    @Override
    public Boolean add(@Valid CompanyDTO companyDTO) {
        return companyService.add(companyDTO);
    }

    @Override
    public Boolean remove(Long id, Long version) {
        // company受外键约束，无法删除
        try {
            return companyService.remove(id, version);
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * 批量删除Company
     *
     * @param idVersionQueryList Company批量删除列表
     * @return 成功返回true
     * @author Yan Huazhi
     * @date 2020/12/15 21:09
     * @version 0.0.1
     */
    @Override
    @ApiLog
    public Boolean removeBatch(List<IdVersionQuery> idVersionQueryList) {
        // company受外键约束，无法删除
//        return companyService.removeBatch(idVersionQueryList);
        try {
            return companyService.removeBatch(idVersionQueryList);
        } catch (Exception e) {

        }
        return false;
    }

    @Override
    public Boolean modify(@Valid CompanyDTO companyDTO) {
        return companyService.modify(companyDTO);
    }

    @ApiLog
    @Override
    public CompanyVO queryCompanyById(@Valid @NotNull Long id) {
        CompanyDTO companyDTO = companyService.queryById(id);
        if (companyDTO != null) {
            CompanyVO companyVO = BeanUtil.copy(companyDTO, CompanyVO.class);
            System.out.println(companyVO);
            return companyVO;
        } else {
            return null;
        }
    }

    /**
     * 复制company记录
     *
     * @param id 公司id
     * @return
     * @author Yan Huazhi
     * @date 2020/12/17 13:10
     * @version 0.0.1
     */
    @Override
    public CompanyDTO copy(Long id) {
        return companyService.copy(id);
    }

    /**
     * @Title:
     * @Description: 批量更新
     * @Author: LiShiLong
     * @Date: 20:36 2020/12/15
     * @return:
     **/
    @Override
    @ApiLog
    public Boolean batchUpdate(@RequestBody List<CompanyDTO> companyDTOS) {
        System.out.println("batchUpdate");
        Boolean flag = true;
        // 批量删除
        for (int i = 0; i < companyDTOS.size(); ++i) {
            CompanyDTO dto = companyDTOS.get(i);
            flag = companyService.batchModify(dto);
            if (flag == false) {
                return false;
            }
        }
        return flag;
    }

    /**
     * 更新单条启用状态
     * @author Yan Huazhi
     * @date 2020/12/18 9:20
     * @version 0.0.1
     * @param companyDTO 公司DTO
     * @return
     */
    @Override
    @ApiLog
    public Boolean status(@RequestBody CompanyDTO companyDTO) {
        return companyService.batchModify(companyDTO);
    }
}
